/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 *
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	libitum
 *
 * <p>描   述  :   ConveyManager.java for com.pengtu.service.admin    </p>
 *
 * <p>最后修改 : $: 2011-7-23-下午05:10:47 v 1.0.0	 libitum   $     </p>
 *
 */

package com.pengtu.service.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.admin.ConveyDao;
import com.pengtu.entity.admin.Convey;
import com.pengtu.entity.app.User;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.model.CoveryModel;
import com.pengtu.service.BaseManager;
import com.pengtu.service.history.HistoryProjectManager;

/**
 * 
 * ConveyManager
 * 
 * 2011-7-23 下午05:10:47
 * 
 * @version 1.0.0
 * 
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class ConveyaManager extends BaseManager {
	private ConveyDao conveyDao;

	private HistoryProjectManager historyProjectManager;

	@Autowired
	public void setConveyDao(ConveyDao conveyDao) {
		this.conveyDao = conveyDao;
	}

	@Autowired
	public void setHistoryProjectManager(
			HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

	/**
	 * 保存
	 * save:
	 * 适用:
	 * @param convey 
	 * @exception 
	 * @since  1.0.0
	 */
	public void save(Convey convey) {
		conveyDao.save(convey);
	}
	
	public void save1(Convey convey) {
		conveyDao.save1(convey);
	}
	
	/**
	 * 判断发文字号是否唯一
	 * isNumUnique:
	 * 适用:
	 * @param newNum
	 * @param oldNum
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public boolean isNumUnique(String newNum,String oldNum){
		return conveyDao.isPropertyUnique("num", newNum, oldNum);
	}
	
	/**
	 * 判断标题是否唯一
	 * isTitleUnique:
	 * 适用:
	 * @param newTitle
	 * @param oldTitle
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public boolean isTitleUnique(String newTitle,String oldTitle){
		return conveyDao.isPropertyUnique("title", newTitle, oldTitle);
	}

	/**
	 * 启动流程
	 * start:
	 * 适用:
	 * @param convey
	 * @param deploymentId
	 * @param historyProject
	 * @param user
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public void start(Convey convey, String deploymentId, User user,String key) {
		Map<String, Object> variables = new HashMap<String, Object>();
		if (StringUtils.isEmpty(convey.getId())) {
			conveyDao.save(convey);
		}
		variables.put(Constants.CONVEY_ID, convey.getId());
		super.start(convey.getId(), deploymentId, key,
				variables);
	}
	
	public void start(Convey convey, String deploymentId, User user,String key,Map<String, Object> variables) {
		if (StringUtils.isEmpty(convey.getId())) {
			conveyDao.save(convey);
		}
		variables.put(Constants.CONVEY_ID, convey.getId());
		System.out.println("======="+variables);
		super.start(convey.getId(), deploymentId, key,
				variables);
	}
	/**
	 * 提交流程
	 * completeTask:
	 * 适用:
	 * @param convey
	 * @param historyProject
	 * @param outcome
	 * @param variables 
	 * @exception 
	 * @since  1.0.0
	 */
	public void completeTask(Convey convey, HistoryProject historyProject,
			String outcome, Map<String, Object> variables) {
		super.completeTask(convey.getTaskId(), outcome, variables);
		historyProjectManager.save(historyProject);
		conveyDao.save(convey);
	}

	/**
	 * 查询待办、待接任务
	 * find:
	 * 适用:
	 * @param user
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public List<Convey> find(User user) {
		List<Convey> list = new ArrayList<Convey>();
		List<Task> tasks1 = super.findGroupTasks(user);
		List<Task> tasks2 = super.findPersonalTasks(user);
		if (tasks1.size() != 0 || tasks1 != null) {
			for (Task task : tasks1) {
				String taskId = task.getId();
				String exectionId = task.getExecutionId();
				String arg = exectionId.split("\\.")[0];
				if ("convey".equals(arg)) {
					String id = (String) super.getVariableByTaskId(taskId,
							Constants.CONVEY_ID);
					Convey convey = conveyDao.get(id);
					if (convey != null) {
						convey.setTaskId(taskId);
						list.add(convey);
					}
				}
			}
		}
		if (tasks2.size() != 0 || tasks2 != null) {
			for (Task task : tasks2) {
				String taskId = task.getId();
				String exectionId = task.getExecutionId();
				String arg = exectionId.split("\\.")[0];
				if ("convey".equals(arg)) {
					String id = (String) super.getVariableByTaskId(taskId,
							Constants.CONVEY_ID);
					Convey convey = conveyDao.get(id);
					if (convey != null) {
						convey.setTaskId(taskId);
						list.add(convey);
					}
				}
			}
		}
		return list;
	}
	
	/**
	 * 判断是否待办
	 * isPersonalTasks:
	 * 适用:
	 * @param user
	 * @param id
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public String isPersonalTasks(User user,String id){
		return super.isPersonalTasks(user, id, Constants.CONVEY_ID);
	}

	/**
	 * 接收任务
	 * takeTask:
	 * 适用:
	 * @param taskId
	 * @param key
	 * @param user
	 * @param historyProject 
	 * @exception 
	 * @since  1.0.0
	 */
	public void takeTask(String taskId, String key, User user,
			HistoryProject historyProject) {
		super.takeTask(taskId, user);
		historyProject.setKey(key);
		historyProjectManager.save(historyProject);
	}

	public Convey get(String id) {
		return conveyDao.get(id);
	}
	
	public List<Convey> get(){
		return conveyDao.getAll();
	}

	/**
	 * 分页查询
	 * findPage:
	 * 适用:
	 * @param page
	 * @param queryFilter
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public Page<Convey> findPage(final Page<Convey> page,
			final QueryFilter queryFilter) {
		return conveyDao.findPage(page, queryFilter);
	}
	
	/**
	 * 删除记录，包括历史记录
	 * delete:
	 * 适用:
	 * @param convey
	 * @param taskId 
	 * @exception 
	 * @since  1.0.0
	 */
	public void delete(Convey convey){
		conveyDao.delete(convey);
	}

	public void createIndexByLucene(String index) {
		
		conveyDao.createIndexByLucene(index);
	}

	public List<CoveryModel> SearchByLucene(String index, String keyword,
			String startDate, String endDate) {
		
		return conveyDao.SearchByLucene(index,keyword,startDate,endDate);
	}
}
